package helpers;

import beans.VideoGameFacade;
import java.io.PrintWriter;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLHelper {

    private XMLHelper() {
    }

    public static void printKeywords(Document document, Element eKeywords, Collection<String> keywords, String category) {
        for (String keyword : keywords) {
            Element eKeyword = document.createElement("keyword");
            eKeyword.setAttribute("category", category);
            eKeyword.appendChild(document.createTextNode(keyword.replaceAll("&", "&amp;")));
            eKeywords.appendChild(eKeyword);
        }
    }

    /**public static void printTags(PrintWriter out, Collection<Tag> tags, VideoGameFacade jeuVideoFacade) {
        try {
            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            Element eTags = document.createElement("tags");
            
            for (Tag tag : tags) {
                int nbJeuxVideo = jeuVideoFacade.countByTag(tag);
                Element eTag = document.createElement("a");
                eTag.setAttribute("href", "?page=tags&amp;id=" + tag.getId());
                eTag.setAttribute("style", "font-size: " + ((15 + nbJeuxVideo) / 4) + "px");
                eTag.appendChild(document.createTextNode(tag.getNom()));
                eTags.appendChild(eTag);
            }

            document.appendChild(eTags);
            out.println("<?xml version=\"1.0\" ?>");
            printDOMTree(out, document);
        } catch (ParserConfigurationException ex) {
            Logger.getLogger(XMLHelper.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
*/
    public static void printDOMTree(PrintWriter out, Node node) {
        int type = node.getNodeType();

        switch (type) {
            case Node.DOCUMENT_NODE: {
                printDOMTree(out, ((Document) node).getDocumentElement());
                break;
            }

            case Node.ELEMENT_NODE: {
                out.print("<");
                out.print(node.getNodeName());
                NamedNodeMap attrs = node.getAttributes();
                for (int i = 0; i < attrs.getLength(); i++) {
                    Node attr = attrs.item(i);
                    out.print(" " + attr.getNodeName()
                            + "=\"" + attr.getNodeValue()
                            + "\"");
                }
                out.print(">");

                NodeList children = node.getChildNodes();
                if (children != null) {
                    int len = children.getLength();
                    for (int i = 0; i < len; i++) {
                        printDOMTree(out, children.item(i));
                    }
                }

                break;
            }

            case Node.ENTITY_REFERENCE_NODE: {
                out.print("&");
                out.print(node.getNodeName());
                out.print(";");
                break;
            }

            case Node.CDATA_SECTION_NODE: {
                out.print("<![CDATA[");
                out.print(node.getNodeValue());
                out.print("]]>");
                break;
            }

            case Node.TEXT_NODE: {
                out.print(node.getNodeValue());
                break;
            }

            case Node.PROCESSING_INSTRUCTION_NODE: {
                out.print("<?");
                out.print(node.getNodeName());
                String data = node.getNodeValue();
                {
                    out.print(" ");
                    out.print(data);
                }
                out.print("?>");
                break;
            }
        }

        if (type == Node.ELEMENT_NODE) {
            out.print("</");
            out.print(node.getNodeName());
            out.print('>');
        }
    }
}
